package birdsong.comm.implemented.tests;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Random;

import birdsong.comm.ClientSideMessage;
import birdsong.comm.implemented.ClientComm;
import birdsong.comm.implemented.ServerComm;
import birdsong.comm.implemented.ServerSideMessage;

public class ClientCommTest {
	
	static ClientComm cliente = new ClientComm();
	static ServerComm servidor = new ServerComm();
   
	private String nickname = getRandomNickName(); 
    private String message = getRandomMessage(); 
    private String locahost = "127.0.0.1";
   
 
    
/**
 * Starts the server    
 */
    @BeforeClass
		public static void IniciaServidor() {
		servidor.start();
	}
/**
 * Tests if it is possible to send a message	
 * @throws UnknownHostException
 * @throws IOException
 */
	@Test
	public void testSendMessage() throws UnknownHostException, IOException {
		cliente.connect(locahost,nickname);
		ServerSideMessage serverMessage = new ServerSideMessage(message, nickname, birdsong.comm.ServerSideMessage.Type.BIRDSONG_MESSAGE);
		cliente.sendMessage(serverMessage);
		cliente.disconnect();
		    
	}
/**
 * Tests the conectivity to the server
 * @throws UnknownHostException
 * @throws IOException
 */
	@Test
	public void testConnect() throws UnknownHostException, IOException {
			cliente.connect(locahost,nickname);
			assertTrue((cliente.isConnected()==true));	
			cliente.disconnect();
			assertTrue((cliente.isConnected() == false));
	}
/**
 * Test to check if a client is connected to the server
 * @throws UnknownHostException
 * @throws IOException
 */
	@Test
	public void testIsConnected() throws UnknownHostException, IOException {
		cliente.connect(locahost, nickname);
		assertTrue((cliente.isConnected()==true));
		cliente.disconnect();
		assertTrue((cliente.isConnected() == false));
	}
/**
 * Test if it is possible to disconect from the server
 */
	@Test
	public void testDisconnect() {
		try {
			cliente.connect(locahost,nickname);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cliente.disconnect();					
		assertTrue((cliente.isConnected()==false));
		
		
		}
/**
 * Tests if it is possible to get a new message from a client	
 */

	@Test
	public void testGetNextMessage() {
		try {
			cliente.connect(locahost,nickname);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//servidor.sendBirdsongMessage(nickname, nickname, message);
		//assertTrue(cliente.getNextMessage()!= null);
		cliente.disconnect();
	}
/**
 * Tests if there is a next message to be processed
 */

	@Test
	public void testHasNextMessage() {
		try {
			cliente.connect(locahost,nickname);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		servidor.sendBirdsongMessage(nickname, nickname, message);
		assertTrue(cliente.hasNextMessage()== false);
		cliente.disconnect();
		
	}
/**
 * Tests sending a new of the type BirdsongMessage
 */
	@Test
	public void testSendBirdsongMessage() {
		try {
			cliente.connect(locahost,nickname);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		cliente.sendBirdsongMessage(message);
		cliente.sendBirdsongMessage(null);
		cliente.disconnect();
	}
/**
 * Tests the follow 
 */
	@Test
	public void testFollow() {
		try {
			cliente.connect(locahost,nickname);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
			cliente.follow(nickname);
			cliente.disconnect();
		

	}
/**
 * Tests the unfollow 
 */
	@Test
	public void testUnfollow() {
		try {
			cliente.connect(locahost,nickname);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String nick = nickname;
		
		cliente.follow(nick);
		cliente.unfollow(nick);
		
		cliente.disconnect();
	}
	
/**
 * Creates a random message
 * @return a new random String message
 */
	private String getRandomMessage(){

		Random r = new Random();
		int wordSize=260;
		int rmin= 'A';
		int rmax='z';
		char[] word = new char[wordSize];
		for (int j = 0; j < wordSize; j++) {
			word[j]= (char) (rmin+ r.nextInt(rmax-rmin));
		}

		return new String(word);

	}
		
/**
 * Creates a Random User Nickname	
 * @return a new random String Nickname
 */
	private String getRandomNickName(){

		Random r = new Random();
		int wordSize=6;
		int rmin= 'A';
		int rmax='z';
		char[] word = new char[wordSize];
		for (int j = 0; j < wordSize; j++) {
			word[j]= (char) (rmin+ r.nextInt(rmax-rmin));
		}
		return new String(word);
	
   }
	
}
